package com.mfl.test;

import com.mfl.calculator.AppleCalculator;
import com.mfl.calculator.MongoCalculator;
import com.mfl.calculator.StrawberryCalculator;

import java.math.BigDecimal;
import java.util.Scanner;

/**
 * @Description: 价格计算
 * @Author: mfl
 * @Date:   2024-03-31
 */
public class CalculatorDemo {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("请输入以下购买顾客: A,B,C,D");
        System.out.println("输入exit退出");
        String customer = scanner.next();
        loop1:
        while (true) {
            switch (customer) {
                case "A":
                    buyAppleAndStrawberry();
                    break loop1;
                case "B":
                    buyAll();
                    break loop1;
                case "C":
                    buyAllAndDiscount();
                    break loop1;
                case "D":
                    buyAllFullReduce();
                    break;
                case "exit":
                    break loop1;
                default:
                    System.out.println("顾客输入错误，请重新输入:");
                    customer = scanner.next();
                    break;
            }
        }
    }

    /**
     * 满100减20
     */
    private static void buyAllFullReduce() {
        try {
            System.out.println("请输入苹果重量:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓重量:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果重量:");
            int mangoWeight = Integer.parseInt(scanner.next());
            System.out.println("水果总价为:" + d(appleWeight, strawberryWeight, mangoWeight));
        } catch (Exception e) {
            System.out.println("输入错误");
        }
    }

    /**
     * 草莓打八折购买
     */
    private static void buyAllAndDiscount() {
        try {
            System.out.println("请输入苹果重量:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓重量:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果重量:");
            int mangoWeight = Integer.parseInt(scanner.next());
            System.out.println("水果总价为:" + c(appleWeight, strawberryWeight, mangoWeight));
        } catch (Exception e) {
            System.out.println("输入错误");
        }
    }

    /**
     * 买三种水果
     */
    private static void buyAll() {
        try {
            System.out.println("请输入苹果重量:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓重量:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入芒果重量:");
            int mangoWeight = Integer.parseInt(scanner.next());
            System.out.println("水果总价为:" + b(appleWeight, strawberryWeight, mangoWeight));
        } catch (Exception e) {
            System.out.println("输入错误");
        }
    }

    /**
     * A顾客买苹果和草莓
     */
    private static void buyAppleAndStrawberry() {
        try {
            System.out.println("请输入苹果重量:");
            int appleWeight = Integer.parseInt(scanner.next());
            System.out.println("请输入草莓重量:");
            int strawberryWeight = Integer.parseInt(scanner.next());
            System.out.println("水果总价为:" + a(appleWeight, strawberryWeight));
        } catch (Exception e) {
            System.out.println("输入错误");
        }
    }

    /**
     * 满减计算
     *
     * @param price
     * @return
     */
    public static Double fullReduce(double price) {
        if (price > 100) {
            return new BigDecimal(String.valueOf(price))
                    .subtract(BigDecimal.valueOf((price / 100) * 20))
                    .doubleValue();

        }
        return price;
    }

    /**
     * a顾客购买
     *
     * @param appleWeight
     * @param strawberryWeight
     * @return
     */
    public static Double a(int appleWeight, int strawberryWeight) {
        Double applePrice = new AppleCalculator().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryCalculator().totalPrice(strawberryWeight);
        return new BigDecimal(applePrice).add(new BigDecimal(strawBerryPrice)).doubleValue();
    }


    /**
     * b顾客购买
     *
     * @param appleWeight
     * @param strawberryWeight
     * @param mangoWeight
     * @return
     */
    public static Double b(int appleWeight, int strawberryWeight, int mangoWeight) {
        Double totalPrice = new AppleCalculator().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryCalculator().totalPrice(strawberryWeight);
        Double mongoPrice = new MongoCalculator().totalPrice(mangoWeight);
        return new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue();
    }

    /**
     * c顾客购买
     *
     * @param appleWeight
     * @param strawberryWeight
     * @param mangoWeight
     * @return
     */
    public static Double c(int appleWeight, int strawberryWeight, int mangoWeight) {
        Double totalPrice = new AppleCalculator().totalPrice(appleWeight);
        //打折后的价格
        Double strawBerryPrice = new StrawberryCalculator().onSalePrice(strawberryWeight);
        Double mongoPrice = new MongoCalculator().totalPrice(mangoWeight);
        return new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue();
    }

    /**
     * d顾客购买
     * @param appleWeight
     * @param strawberryWeight
     * @param mangoWeight
     * @return
     */
    public static Double d(int appleWeight, int strawberryWeight, int mangoWeight) {
        Double totalPrice = new AppleCalculator().totalPrice(appleWeight);
        Double strawBerryPrice = new StrawberryCalculator().totalPrice(strawberryWeight);
        Double mongoPrice = new MongoCalculator().totalPrice(mangoWeight);
        return fullReduce(new BigDecimal(totalPrice)
                .add(new BigDecimal(strawBerryPrice))
                .add(new BigDecimal(mongoPrice))
                .doubleValue());
    }
}



























